package com.atguigu.JVMDay02;

import java.lang.ref.WeakReference;
import java.util.concurrent.TimeUnit;

//弱引用
public class WeakReferenceDemo {
    public static void main(String[] args) {
        //不管内存是否足够，都会被回收
        WeakReference<User> weakReference = new WeakReference<>(new User(20,"zml"));
        System.out.println(weakReference.get());
        System.gc();
        //线程睡眠几秒
        try {
            TimeUnit.SECONDS.sleep(1);} catch (InterruptedException e) {throw new RuntimeException(e);}
        System.out.println("After:");
        System.out.println(weakReference.get());//被回收了，所以是null


    }
}
